package com.chainyoung.component_tmperson.ui.mvp.presenter;

import com.chainyoung.common.appcommon.entity.RecieveAccount;
import com.chainyoung.common.appcommon.entity.UploadFile;
import com.chainyoung.common.base.CommonSubscriber;
import com.chainyoung.common.bean.UserInfo;
import com.chainyoung.common.di.scope.ActivityScope;
import com.chainyoung.common.mvp.BasePresenter;
import com.chainyoung.common.utils.RxUtils;
import com.chainyoung.common.utils.UpLoadUtils;
import com.chainyoung.component_tmperson.ui.mvp.contract.AccountInfoContract;
import com.chainyoung.component_tmperson.ui.mvp.contract.UpdateUserInfoContract;

import java.util.Map;
import java.util.TreeMap;

import javax.inject.Inject;

import okhttp3.MultipartBody;
import okhttp3.RequestBody;

@ActivityScope
public class UpdateUserInfoPresenter extends BasePresenter<UpdateUserInfoContract.Model, UpdateUserInfoContract.View> {

    @Inject
    public UpdateUserInfoPresenter(UpdateUserInfoContract.Model model, UpdateUserInfoContract.View rootView) {
        super(model, rootView);
    }


    public void updateUserInfo(TreeMap<String, Object> map, boolean isAfterUpdateLogo) {
        addSubscribe(mModel.updataUserInfo(map)
                .compose(RxUtils.<UserInfo>rxSchedulerHelper())
                .subscribeWith(new CommonSubscriber<UserInfo>(mView, !isAfterUpdateLogo) {
                    @Override
                    public void onNext(UserInfo data) {
                        mView.updataUserInfoSuccess(data);
                        mView.hideLoading();
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        mView.hideLoading();
                    }

                    @Override
                    public void onComplete() {
                        super.onComplete();
                        mView.hideLoading();
                    }
                }));
    }


    public void updateUserLogo(String path, int type) {
        //Map<String, RequestBody> map=UpLoadUtils.getBody(path,type);
        RequestBody body = UpLoadUtils.getBody(path, type);
        addSubscribe(mModel.upLoadLogo(body)
                .compose(RxUtils.<UploadFile>rxSchedulerHelper())
                .subscribeWith(new CommonSubscriber<UploadFile>(mView, false) {
                    @Override
                    protected void onStart() {
                        super.onStart();
                        mView.showLoading("");
                    }

                    @Override
                    public void onNext(UploadFile data) {
                        mView.uploadImgSuccess(data);
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                    }

                    @Override
                    public void onComplete() {
                        //super.onComplete();
                    }
                }));
    }


}

